package com.wrx.swing;

import com.wrx.SwingApplication;
import com.wrx.service.generatesql.GenerateService;
import com.wrx.service.mndata.MnDataService;
import com.wrx.swing.panel.*;
import com.wrx.swing.menu.MenuBarFactory;
import com.wrx.swing.panel.generate.AddDevicePointPanel;
import com.wrx.swing.panel.generate.CodeToEnglishPanel;
import com.wrx.swing.panel.generate.SqlGeneratePanel;
import com.wrx.swing.panel.mndata.MnRawDataPanel;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import java.awt.*;

import static com.wrx.swing.menu.MenuConfig.*;


public class SwingWindow extends JFrame {

    private final GenerateService generateService;
    private final MnDataService mnDataService;
    private JPanel mainPanel; // 主卡片区域
    private CardLayout cardLayout; // 用于切换界面

    /**
     * 构造函数：接收 Spring ApplicationContext 并注入依赖
     * @param ctx Spring 应用上下文
     */
    public SwingWindow(ApplicationContext ctx) {
        // 1️⃣ 从 Spring 容器获取 Service Bean
        this.generateService = ctx.getBean(GenerateService.class);
        this.mnDataService = ctx.getBean(MnDataService.class);
        // 2️⃣ 基础窗口设置
        windowBaseConfig();
        // 3️⃣ 中间主界面（卡片切换）
        middleCardConfig();
        // 4️⃣ 初始化菜单并绑定切换菜单动态更新标题
        MenuBarFactory.attachToFrame(this, () -> cardLayout, () -> mainPanel);
    }


    /**
     * 中间主界面（卡片切换）
     */
    private void middleCardConfig() {
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
        
        // 创建面板实例
        CodeToEnglishPanel codeToEnglishPanel = new CodeToEnglishPanel(generateService, this);
        AddDevicePointPanel addDevicePointPanel = new AddDevicePointPanel(generateService, this);
        SqlGeneratePanel sqlGeneratePanel = new SqlGeneratePanel(generateService, this);
        MnRawDataPanel mnRawDataPanel = new MnRawDataPanel(mnDataService, this);
        ConfigPanel configPanel = new ConfigPanel();
        AboutPanel aboutPanel = new AboutPanel();

        // 模拟数据工具
        mainPanel.add(mnRawDataPanel.createPanel(), MAKE_RAW_DATA.getCardName());

        // 物联网数据转换SQL工具库
        mainPanel.add(codeToEnglishPanel.createPanel(), CODE_TO_ENGLISH.getCardName());
        mainPanel.add(addDevicePointPanel.createPanel(), CODE_TO_ENGLISH_ADD_DEVICE_POINT.getCardName());
        mainPanel.add(sqlGeneratePanel.createPanel(), GENERATE_SQL.getCardName());

        // 系统
        mainPanel.add(configPanel.createPanel(), "CONFIG");
        mainPanel.add(aboutPanel.createPanel(), "ABOUT");
        add(mainPanel, BorderLayout.CENTER);
    }

    /**
     * 基础窗口设置
     */
    private void windowBaseConfig() {
        setTitle("工具集");                        // 设置窗口标题
        setSize(900, 700);           // 设置窗口默认大小（宽900，高600）
        setLocationRelativeTo(null);              // 窗口居中显示（相对于屏幕中心）
        setDefaultCloseOperation(EXIT_ON_CLOSE);  // 点击关闭按钮时退出应用
        setLayout(new BorderLayout());            // 使用 BorderLayout 布局管理器
    }


    public static void main(String[] args) {
        // 设置系统属性，确保 Swing 应用在非 headless 模式下运行
        System.setProperty("java.awt.headless", "false");
        
        // 启动 Spring 容器（禁用 Web 服务器）
        SpringApplication app = new SpringApplication(SwingApplication.class);
        app.setWebApplicationType(org.springframework.boot.WebApplicationType.NONE);
        ApplicationContext ctx = app.run(args);
        
        // 在 Swing 事件分发线程中创建并显示窗口
        SwingUtilities.invokeLater(() -> {
            SwingWindow window = new SwingWindow(ctx);
            window.setVisible(true);
        });
    }
}